/*
 * ViewerFrame.java
 *
 * Created on 26 December 2008, 17:17
 */

package ui.components.standard.log;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Collection;
import java.util.LinkedList;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import ui.CircuitPanel;
import ui.components.SelectableComponent;
import ui.components.standard.PinLogger;

/**
 *
 * @author  matt
 */
public class ViewerFrame extends javax.swing.JFrame {
    private CircuitPanel circuit;
    private int yOffset=0;
    private LinkedList<PinLogger> loggers = new LinkedList<PinLogger>();
    private Long startTime = Long.MAX_VALUE;
    private Long endTime = 0l;
    
    /** Creates new form ViewerFrame */
    public ViewerFrame(CircuitPanel circuit) {
        this.circuit = circuit;
        initComponents();

        viewerpanel.addComponentListener(new ComponentAdapter(){
            @Override
            public void componentResized(ComponentEvent e) {
                 PanelScrollPane.getViewport().setViewPosition(new Point(viewerpanel.getWidth(),0));
            }
        });
        
        PanelScrollPane.getHorizontalScrollBar().setUnitIncrement(20);
        ((ViewerPanel)viewerpanel).getRowHeader().setPreferredSize(new Dimension(35, 500));
        PanelScrollPane.setRowHeaderView(((ViewerPanel)viewerpanel).getRowHeader());
    }
    
    public sim.SimulatorStateListener getSimStateListener(){
        return (ViewerPanel)viewerpanel;
    }
    
    public void addPinLogger(PinLogger pl){
        if(!loggers.contains(pl)){
            loggers.add(pl);
            if(pl.getStartTime()<startTime){
                startTime = pl.getStartTime();
            }
            drawCheckBoxes();
        }
    }
    
    public void addPinLoggers(Collection<PinLogger> cpl){
        for(final PinLogger pl: cpl){  
            addPinLogger(pl);
        }
    }
    
    public void clearPinLoggers(){
        for(PinLogger pl: loggers){
            pl.clear();
        }
        loggers.clear();
        drawCheckBoxes();
    }

    public void removePinLogger(SelectableComponent sc) {
        loggers.remove(sc);
        drawCheckBoxes();
    }

    private void drawCheckBoxes(){
        CheckboxPanel = new JPanel();
        CheckboxPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        CheckBoxScrollPane.setViewportView(CheckboxPanel);
        yOffset = 0;
        for(final PinLogger pl: loggers){
            JCheckBox newCheckBox = new JCheckBox();
            newCheckBox.setText(pl.getLabel()); 
            newCheckBox.setSelected(pl.isEnabled());
            newCheckBox.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    pl.setEnabled(!pl.isEnabled());
                }
            });
            CheckboxPanel.add(newCheckBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, yOffset, -1, -1));

            yOffset += 20;
        }
    }
    
    public LinkedList<PinLogger> getLoggers(){
        return loggers;
    }
    
    public Long getEndTime() {
        return endTime;
    }

    public void setEndTime(Long endTime) {
        this.endTime = endTime;
    }

    public Long getStartTime() {
        return startTime;
    }

    public void setStartTime(Long startTime) {
        this.startTime = startTime;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Toolbar = new javax.swing.JToolBar();
        Save = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        ClearLog = new javax.swing.JButton();
        PanelScrollPane = new javax.swing.JScrollPane();
        viewerpanel = new ui.components.standard.log.ViewerPanel(this);
        CheckBoxScrollPane = new javax.swing.JScrollPane();
        CheckboxPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("ui/Bundle"); // NOI18N
        setTitle(bundle.getString("ViewerFrame.title")); // NOI18N

        Toolbar.setRollover(true);

        Save.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ui/images/buttons/toolbar/gtk-edit.png"))); // NOI18N
        Save.setFocusable(false);
        Save.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        Save.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        Save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveActionPerformed(evt);
            }
        });
        Toolbar.add(Save);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ui/images/buttons/toolbar/document-properties.png"))); // NOI18N
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        Toolbar.add(jButton3);
        Toolbar.add(jSeparator1);

        ClearLog.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ui/images/buttons/toolbar/appointment-new.png"))); // NOI18N
        ClearLog.setFocusable(false);
        ClearLog.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        ClearLog.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        ClearLog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ClearLogActionPerformed(evt);
            }
        });
        Toolbar.add(ClearLog);

        getContentPane().add(Toolbar, java.awt.BorderLayout.NORTH);

        org.jdesktop.layout.GroupLayout viewerpanelLayout = new org.jdesktop.layout.GroupLayout(viewerpanel);
        viewerpanel.setLayout(viewerpanelLayout);
        viewerpanelLayout.setHorizontalGroup(
            viewerpanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 803, Short.MAX_VALUE)
        );
        viewerpanelLayout.setVerticalGroup(
            viewerpanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 336, Short.MAX_VALUE)
        );

        PanelScrollPane.setViewportView(viewerpanel);

        getContentPane().add(PanelScrollPane, java.awt.BorderLayout.CENTER);

        CheckboxPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        CheckBoxScrollPane.setViewportView(CheckboxPanel);

        getContentPane().add(CheckBoxScrollPane, java.awt.BorderLayout.EAST);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void ClearLogActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClearLogActionPerformed
    ((ViewerPanel) viewerpanel).clear();
    viewerpanel.repaint();
}//GEN-LAST:event_ClearLogActionPerformed

private void SaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveActionPerformed
    for(PinLogger pl: loggers){
        pl.print();
    }
}//GEN-LAST:event_SaveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane CheckBoxScrollPane;
    private javax.swing.JPanel CheckboxPanel;
    private javax.swing.JButton ClearLog;
    private javax.swing.JScrollPane PanelScrollPane;
    private javax.swing.JButton Save;
    private javax.swing.JToolBar Toolbar;
    private javax.swing.JButton jButton3;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JPanel viewerpanel;
    // End of variables declaration//GEN-END:variables

}
